package com.usermanage.controller;

import com.usermanage.Service.QuotaService;
import com.usermanage.beanParam.QuotaBean;
import com.usermanage.util.JsonData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 *SKU数据操作接口
 * searchQuota    根据组查询组下所有定额
 * addQuota   根据组id添加定额详情
 *
 */
@Api(description = "定额数据操作接口")
@RestController
@RequestMapping("/quota")
public class QuotaController {

    @Resource
    private QuotaService quotaService;

    /**
     * 根据组id查询组下的合作定额
     * @param groupId  组织id
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "根据组查询组下所有定额" ,  notes="根据组查询组下所有定额")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "groupId", value = "组id", required = true,  paramType = "query", dataType = "String")
    })
    @RequestMapping(value="/searchQuota",method= RequestMethod.GET)
    public JsonData searchQuota(@RequestParam("groupId") String groupId)throws Exception{
        List list = quotaService.searchQuota(groupId);
        return JsonData.success(list);
    }

    /**
     * 根据组id添加定额详情
     * @param bean  定额对象
     * @param bindingResult  验证结果
     * @return
     */
    @ApiOperation(value = "根据组id添加定额详情" ,  notes="根据组id添加定额详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "groupId", value = "组id", required = true,  paramType = "query", dataType = "String")
    })
    @RequestMapping(value="/creatQuota",method= RequestMethod.POST)
    public JsonData creatQuota(@Valid @RequestBody QuotaBean bean, BindingResult bindingResult){
        //1、验证提交数据是否合法
        if(bindingResult.hasErrors()){
            return JsonData.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        //2、验证是否有该定额
        Boolean bool = quotaService.isExistQuota("");
        if (bool){
            return JsonData.fail("该定额已存在无需添加");
        }
        //3、创建定额
        int result = quotaService.creatQuota(bean);
        if (result==0){
            return JsonData.fail("定额创建失败，系统异常。");
        }
        return JsonData.success();
    }




//    @ApiOperation(value = "根据sku,定额计算出相应的销售总价格" ,  notes="根据sku,定额计算出相应的销售总价格")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "skuId", value = "skuId", required = true,  paramType = "query", dataType = "String")
//    })
//    @RequestMapping(value="/calculateQuota",method= RequestMethod.POST)
//    public JsonData calculateQuota(@RequestParam("skuId") String skuId){
//        //1、根据skuId获取sku信息详情
//        SkuParam skuParam = null;
//        //2、根据定额处理sku数据
//        SkuParam skuParamResult = quotaService.calculateQuota(skuParam);
//        return JsonData.success(skuParamResult);
//    }


}
